package com.yanqu.road.server.manger.union.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.union.honors.eHonorsActivityType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.cross.CrossUnionHonorsProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

public class CrossUnionHonorsMgr extends TempMgr {

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void addHonors(ActivityInfo activityInfo, long playerId, String unionUid){
        eHonorsActivityType activityType = eHonorsActivityType.getHonorsType(activityInfo.getType(), activityInfo.getChildType());
        if(activityType == null){
            return;
        }

        int honors = getHonorsByActivity(activityInfo.getServerIdList().size(), activityType);
        if(honors == 0){
            return;
        }

        //跨服增加历史荣誉
        CrossUnionHonorsProto.CrossUnionGetHonorsUploadMsg.Builder reqMsg = CrossUnionHonorsProto.CrossUnionGetHonorsUploadMsg.newBuilder();
        reqMsg.setUnionUid(unionUid);
        reqMsg.setActivityId(activityInfo.getActivityId());
        CrossUnionHonorsProto.UnionHistoryHonorsRecordTemp.Builder builder = CrossUnionHonorsProto.UnionHistoryHonorsRecordTemp.newBuilder();
        builder.setHonors(honors);
        builder.setGetTime(DateHelper.getCurrentSecond());
        builder.setActivityType(activityInfo.getType());
        builder.setActivityChildType(activityInfo.getChildType());
        builder.addAllServerList(activityInfo.getServerIdList());
        reqMsg.setRecord(builder);
        GamePlayerMgr.sendPacket(playerId, YanQuMessageUtils.buildMessage(Protocol.C_UNION_HONORS_GET_UPLOAD, reqMsg));
        
    }

    //新版本的变成道具的
    public static void addHonorsItemRecord(int activityId,int acType,int acChildType,int rank,long userId, String unionUid,long honors,List<Long> serverIdList,int itemId){
        if(honors == 0){
            return;
        }
        //跨服增加历史荣誉
        CrossUnionHonorsProto.CrossUnionGetHonorsUploadMsg.Builder reqMsg = CrossUnionHonorsProto.CrossUnionGetHonorsUploadMsg.newBuilder();
        reqMsg.setUnionUid(unionUid);
        reqMsg.setActivityId(activityId);
        CrossUnionHonorsProto.UnionHistoryHonorsRecordTemp.Builder builder = CrossUnionHonorsProto.UnionHistoryHonorsRecordTemp.newBuilder();
        builder.setHonors((int)honors);
        builder.setGetTime(DateHelper.getCurrentSecond());
        builder.setActivityType(acType);
        builder.setActivityChildType(acChildType);
        builder.addAllServerList(serverIdList);
        builder.setRank(rank);
        builder.setGoodId(itemId);
        builder.setActivityId(activityId);
        reqMsg.setRecord(builder);
        GamePlayerMgr.sendPacket(userId, YanQuMessageUtils.buildMessage(Protocol.C_UNION_HONORS_ITEM_ADD, reqMsg));

    }

    private static int getHonorsByActivity(int serverListSize, eHonorsActivityType activityType) {
        int honors = 0;
        String rankInfo = null;
        List<String> typeParamList = StringUtils.stringToStringList(GameConfig.BANNER_RANK_HONOR_REWARD, "\\|");
        for (String typeParam : typeParamList) {
            List<String> typeList = StringUtils.stringToStringList(typeParam, "#");
            int type = Integer.valueOf(typeList.get(0));
            if(type == activityType.getHonorsType()){
                rankInfo = typeList.get(1);
            }
        }

        if(!StringUtils.isNullOrEmpty(rankInfo)) {
            List<String> rankList = StringUtils.stringToStringList(rankInfo, ";");
            for (String rank : rankList) {
                List<Integer> itemList = StringUtils.stringToIntegerList(rank, ",");
                if (serverListSize >= itemList.get(0) && serverListSize <= itemList.get(1)) {
                    honors = itemList.get(2);
                }
            }
        }
        return honors;
    }

}
